<?php
/*由bean脚本生成 powerBy--joe*/
namespace app\common\manage;
use app\common\bean\OrderTotalBean;
use app\common\bean\SystemRechargeBean;
use app\common\bean\UserBean;
use app\common\bean\UserVipBean;
use app\common\enum\OrderTotalPayTypeEnum;
use app\common\enum\OrderTotalTypeEnum;
use app\common\enum\UserRechargeStatusEnum;
use app\common\model\ConfigModel;
use app\common\model\OrderTotalModel;
use app\common\model\SystemRechargeModel;
use app\common\model\UserModel;
use app\common\model\UserVipModel;
use app\common\utils\CommonUtil;
use app\common\utils\CurlUtil;
use app\common\validate\UserRechargeValidate;
use app\common\model\UserRechargeModel;
use app\common\bean\UserRechargeBean;
use app\common\bean\ListMap;
use app\common\utils\TimeUtil;
use app\common\exception\MyException;
use Exception;
class UserRechargeManage extends BaseManage
{
	/**
	* @return UserRechargeManage
	*/
	static function getInstance(){
		return parent::getInstance();
	}
	public function callBack($recharge){
        if($recharge[UserRechargeBean::$status]==UserRechargeStatusEnum::WAITPAY){
            UserRechargeModel::getInstance()->updateColumn(UserRechargeBean::$id, $recharge[UserRechargeBean::$id], UserRechargeBean::$status, UserRechargeStatusEnum::SUCCESSPAY);

            $tdata[OrderTotalBean::$orderNumber] = $recharge[UserRechargeBean::$orderNumber];
            $tdata[OrderTotalBean::$userId] = $recharge[UserRechargeBean::$userId];
            $tdata[OrderTotalBean::$type] = OrderTotalTypeEnum::RECH;
            $tdata[OrderTotalBean::$transactionPrice] = $recharge[UserRechargeBean::$payPrice];
            $tdata[OrderTotalBean::$otherPrice] = $recharge[UserRechargeBean::$givePrice];
            $tdata[OrderTotalBean::$realPrice] = $recharge[UserRechargeBean::$payPrice];
            $tdata[OrderTotalBean::$payType] = OrderTotalPayTypeEnum::WXPAY;
            $tdata[OrderTotalBean::$blance] = $recharge['blance'];
	        OrderTotalModel::getInstance()->insert( $tdata );

            $where[UserBean::$id] = $recharge[UserRechargeBean::$userId];
	        UserModel::getInstance()->where($where)->setInc('blance', $recharge[UserRechargeBean::$realPrice]);

            $vip_config = ConfigModel::getInstance()->where(['config_key'=>'vip_price'])->find();
            if($vip_config['value']<=$recharge[UserRechargeBean::$payPrice]){
                $vdata[UserVipBean::$userId] = $recharge[UserRechargeBean::$userId];
                $vdata[UserVipBean::$vipPrice] = $vip_config['value'];
                $vdata[UserVipBean::$vipStartTime] = date('Y-m-d');
                $vdata[UserVipBean::$vipEndTime] = date('Y-m-d', strtotime('+1 year'));
                $vdata[UserVipBean::$payPrice] = $recharge[UserRechargeBean::$payPrice];
                UserVipModel::getInstance()->insert($vdata);
                echo UserVipModel::getInstance()->getLastSql();
            }

            $mess_data = [
                'first'=>['value'=>'会员充值'],
                'keyword1'=>['value'=>$recharge[UserRechargeBean::$payPrice].'元'],
                'keyword2'=>['value'=>'会员充值'],
                'keyword3'=>['value'=>'会员充值'],
                'keyword4'=>['value'=>'用户'],
            ];
            CurlUtil::getInstance()->templateMess('o4ZdX6BDx_mEJ8QeQ5UNcfVmhRr8', '9Af_uC3pbQJ1MCHe97jRG7ycoswyg0REUkZ_Qv1v0mI', $mess_data);
        }

    }
    public function pay(UserRechargeBean $userRechargeBean){
        $data['out_trade_no'] = $userRechargeBean->getOrderNumber();
        $data['total_fee'] = $userRechargeBean->getRealPrice();
        $data['body'] = '照相馆充值';
        $data['tag'] = '充值';
        $data['notify_url'] = request()->domain().'/api/Recharge/callBack';
        $data['openid'] = $userRechargeBean->getParameter('openid');

        return CommonUtil::getInstance()->weixinPay( $data );
    }
    /**
     * 计算到账数据
     * @param  $listMap
     * @return $give_money 赠送金额
     */
    public function getAccount($money){
        $where[] = [SystemRechargeBean::$quota, '<=', $money];
        $info = SystemRechargeModel::getInstance()->selectData($where, false, SystemRechargeBean::$quota.' DESC');
        return $info ? $info[SystemRechargeBean::$giveMoney] : 0;

    }
	/**
	 * @desc 获取分页数据
	 * @param ListMap $listMap
	 * @return ListMap $listMap
	 * @throws Exception
	**/
	public function getPaginatorList(ListMap $listMap) {
		$conditionMap = new ListMap();
		if($timeArray = TimeUtil::getInstance()->dealBetweenTime( $listMap )){
            $conditionMap->setParameter(UserRechargeBean::$createdAt,$timeArray);
        }

		$conditionMap->setAlias(UserRechargeBean::$alias);
		$listMap->setField(UserRechargeBean::$alias.ALL_FIELD);
		!$this->getParamBool($listMap,"orderFieldName") ?: $conditionMap->setOrder($listMap->getParameter('orderFieldName').SPACE.$listMap->getParameter('orderType'));
		$conditionMap->setWith("user");
		$conditionMap->setParameter(UserRechargeBean::$status,$listMap->getParameter(UserRechargeBean::$status));
		$resultMap = UserRechargeModel::getInstance()->getList($conditionMap);
		$resultMap?:MyException::throwMyException("查询数据失败");
		return $resultMap;
	}
	
	/**
	 * @desc 添加数据
	 * @param UserRechargeBean $userRechargeBean
	 * @return int int
	 * @throws Exception
	**/
	public function add($userRechargeBean) {
        $give_money = $this->getAccount($userRechargeBean->getPayPrice());

        $userRechargeBean->setOrderNumber(CommonUtil::getInstance()->getUnique('re'));
        $userRechargeBean->setGivePrice( $give_money );
        $userRechargeBean->setRealPrice( $give_money+$userRechargeBean->getPayPrice() );
        $userRechargeBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
		$result = UserRechargeModel::getInstance()->addDataGetInsertId($userRechargeBean->getData());
		$result ?: MyException::throwMyException("插入数据失败");
		return $userRechargeBean;
	}
	
	/**
	 * @desc 编辑数据
	 * @param UserRechargeBean $userRechargeBean
	 * @return int int
	 * @throws Exception
	**/
	public function edit($userRechargeBean) {
		UserRechargeValidate::getInstance()->goCheck(UserRechargeValidate::$edit,$userRechargeBean->getData());
		// Todo:添加业务处理代码...


		$result = UserRechargeModel::getInstance()->updatedData([UserRechargeBean::$id => $userRechargeBean->getId()],$userRechargeBean->getData());
		$result ?: MyException::throwMyException("更新失败");
		return $result;
	}
	
	/**
	 * @desc 删除数据
	 * @param UserRechargeBean $userRechargeBean
	 * @return int int
	 * @throws Exception
	**/
	public function delete($userRechargeBean) {
		UserRechargeValidate::getInstance()->goCheck(UserRechargeValidate::$pkId,$userRechargeBean->getData());
		// Todo:添加业务处理代码...


		$result = UserRechargeModel::getInstance()->deleteData([UserRechargeBean::$id => $userRechargeBean->getId()]);
		$result ?: MyException::throwMyException("删除失败");
		return $result;
	}
	
	/**
	 * @desc 根据主建获取数据
	 * @param UserRechargeBean $userRechargeBean
	 * @return UserRechargeBean $userRechargeBean
	 * @throws Exception
	**/
	public function getInfo($userRechargeBean) {
		UserRechargeValidate::getInstance()->goCheck(UserRechargeValidate::$pkId,$userRechargeBean->getData());
		// Todo:添加业务处理代码...


		$result = UserRechargeModel::getInstance()->selectData([UserRechargeBean::$id => $userRechargeBean->getId()]);
		$result ?: MyException::throwMyException("获取信息失败");
		$userRechargeBean->setData($result);
		return $userRechargeBean;
	}
}